#ifdef _WIN32 
#include <windows.h> 
#endif

#include "DecisionTree.h"

DecisionTree::DecisionTree(DecisionTree *_root, Entity *_entity, bool _final,
		unsigned int _id)
{
	entity = _entity;
	root = _root;
	final = _final;
	id = _id;
	no = NULL;
	yes = NULL;
}

DecisionTree::DecisionTree(Entity *_entity, bool _final, unsigned int _id)
{
	entity = _entity;
	root = NULL;
	final = _final;
	id = _id;
	yes = NULL;
	no = NULL;
}

DecisionTree::~DecisionTree(void)
{

}

void DecisionTree::setYes(DecisionTree *_yes)
{
	yes = _yes;
}

void DecisionTree::setNo(DecisionTree *_no)
{
	no = _no;
}

DecisionTree* DecisionTree::getYes()
{
	return yes;
}

DecisionTree* DecisionTree::getNo()
{
	return no;
}

unsigned int DecisionTree::getDecision()
{
	//Get unit decision for specific leaf of tree
	//This method needs to be overriden in each entity using Decision Tree to perform evaluation of leaf according to id
	//Method should perform decision based on real facts(known things(speed, range, etc..)) & optionaly some random

	unsigned int dec = getUnitDecision(id);

	if (dec == 1 && final != true && yes != NULL)
	{
		//call yes node
		return yes->getDecision();

	}
	else if (dec == 0 && final != true && no != NULL)
	{
		//call no node
		return no->getDecision();
	}
	else if (final)
	{
		//Return final decision. Need's to be defined somewhere else statically
		//Or possibly call method which set units action based on returning id.
		return id;
	}
	else
	{
		//log error, something has gone wrong. Return 999 in case of failure
		return 999;
	}

}

unsigned int DecisionTree::getUnitDecision(unsigned int id)
{
	return entity->resolveDecision(id);
}
